<script setup>
import OImage from "../../setting/Images.vue";
import PrizePopupSelect from "../../setting/PrizePopupSelect.vue"

const props = defineProps({
  config: {
    type: Object,
    required: true,
  },
  name: {
    type: String,
    defalut: "",
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  group: {
    type: Array,
    default: () => [],
  },
});

const emits = defineEmits(["update:config", "change"])

const form = ref({
  $position: { top: 0, left: 0 },
  $size: { width: 355, height: 517 },
  $removeable: true,
  bgImg: "./imagesStatic/activity/gashapon_bg.png",
  ballBgPosition: {
    top: 128,
    left: 30,
  },
  ballBgSize: {
    width: 269,
    height: 170,
  },
  actionImg: "./imagesStatic/activity/gashapon_btn.png",
  actionSize: {
    width: 189,
    height: 59,
  },
  actionPosition: {
    left: 90,
    top: 328,
  },
  windowImg: "./imagesStatic/activity/gashapon_window.png",
  windowSize: {
    width: 81,
    height: 85,
  },
  windowPosition: {
    left: 140,
    top: 410,
  },
  ball: [
    "./imagesStatic/activity/gashapon_ball1.png",
    "./imagesStatic/activity/gashapon_ball2.png",
    "./imagesStatic/activity/gashapon_ball3.png",
  ],
  ballSize: {
    width: 69,
    height: 69,
  },
});

const formRef = ref();

const detail = computed({
  get: () => {
    return Object.assign({}, form.value, props.config);
  },
  set: (val) => {
    emits("update:config", val);
    emits("change", val);
  },
});

const onChange = (val, type) => {
  detail.value = detail.value;
};

// 表单校验
const check = () => {
  return new Promise((resolve, reject) => {
    formRef.value.validate(async (valid, invalidFields) => {
      if (valid) {
        resolve();
      } else
        reject(
          `${props.name}校验未通过，${
            invalidFields[Object.keys(invalidFields)[0]][0].message
          }`
        );
    });
  });
};

onMounted(() => {
  nextTick(() => {
    onChange();
  });
});

defineExpose({ check });
</script>
<template>
  <el-form
    ref="formRef"
    :model="detail"
    :disabled="disabled"
    label-width="auto"
  >
    <PrizePopupSelect v-model="detail" :group="group" @change="onChange" />
    <OImage
      v-model="detail.bgImg"
      v-model:size="detail.$size"
      name="扭蛋机图片"
      @change="onChange"
    />
    <OImage
      v-model="detail.actionImg"
      v-model:size="detail.actionSize"
      name="抽奖按钮"
      @change="onChange"
    />
    <OImage
      v-model="detail.windowImg"
      v-model:size="detail.windowSize"
      name="出蛋口"
      @change="onChange"
    />
    <OImage
      v-model="detail.ball"
      v-model:size="detail.ballSize"
      name="扭蛋"
      @change="onChange"
    />
  </el-form>
</template>
